package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryPositionGradeListReq;
import com.sg.dto.base.framework.req.QueryPositionGradeDetailReq;
import com.sg.dto.base.framework.req.QueryPositionGradeListReq;
import com.sg.entity.PositionGrade;
import com.sg.service.base.framework.MPositionGradeService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.PositionGradeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MPositionGradeServiceImpl
        extends ServiceImpl<PositionGradeMapper, PositionGrade>
        implements MPositionGradeService {

    @Autowired
    PositionGradeMapper positionGradeMapper;

    /**
     * 1-3-03批量修改职务级别[1827]
     * gen by moon at 10/16/2022, 11:07:10 AM
     */
    @Trace(operationName = "1-3-03批量修改职务级别")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdatePositionGrade(List<PositionGrade> positionGrade) {

        if (positionGrade == null || positionGrade.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(positionGrade);

    }

    /**
     * 1-3-03批量删除职务级别[1813]
     * gen by moon at 10/16/2022, 11:07:10 AM
     */
    @Trace(operationName = "1-3-03批量删除职务级别")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeletePositionGrade(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-3-03批量查询职务级别列表[1482]
     * gen by moon at 10/16/2022, 11:07:09 AM
     */
    @Trace(operationName = "1-3-03批量查询职务级别列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PositionGrade> batchQueryPositionGradeList(BatchQueryPositionGradeListReq reqDto) {

        try {
            QueryWrapper<PositionGrade> queryWrapper = new QueryWrapper<>();

            if (reqDto.getOrgPositionGradeList() == null || reqDto.getOrgPositionGradeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("position_grade_id", reqDto.getOrgPositionGradeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PositionGrade.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PositionGrade.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PositionGrade.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<PositionGrade>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-03查询职务级别列表[137]
     * gen by moon at 10/16/2022, 11:07:08 AM
     */
    @Trace(operationName = "1-3-03查询职务级别列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PositionGrade> queryPositionGradeList(QueryPositionGradeListReq reqDto) {

        try {
            QueryWrapper<PositionGrade> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PositionGrade.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PositionGrade.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PositionGrade.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<PositionGrade>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-03查询职务级别详情[136]
     * gen by moon at 10/16/2022, 11:07:07 AM
     */
    @Trace(operationName = "1-3-03查询职务级别详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PositionGrade queryPositionGradeDetail(QueryPositionGradeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<PositionGrade> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<PositionGrade> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                PositionGrade retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-03删除职务级别[135]
     * gen by moon at 10/16/2022, 11:07:06 AM
     */
    @Trace(operationName = "1-3-03删除职务级别")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePositionGrade(String positionGradeId) {

        return super.removeById(positionGradeId);
    }

    /**
     * 1-3-03修改职务级别[134]
     * gen by moon at 10/16/2022, 11:07:06 AM
     */
    @Trace(operationName = "1-3-03修改职务级别")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePositionGrade(PositionGrade positionGrade) {
        positionGrade.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        positionGrade.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(positionGrade);
    }

    /**
     * 1-3-03新增职务级别[133]
     * gen by moon at 10/16/2022, 11:07:05 AM
     */
    @Trace(operationName = "1-3-03新增职务级别")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addPositionGrade(PositionGrade positionGrade) {
        positionGrade.setPositionGradeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        positionGrade.setBatchEndTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        positionGrade.setSubjectLifeCycle("EDITING");
        positionGrade.setIsArchive("FALSE");
        positionGrade.setArchivngType("NOT_ARCHIVE");
        positionGrade.setIsEditing("TRUE");
        positionGrade.setIsUpdate("TRUE");
        positionGrade.setUpdateStatus("NEW");
        positionGrade.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        positionGrade.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        positionGrade.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        positionGrade.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        positionGrade.setIsValid("FALSE");
        positionGrade.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        positionGrade.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(positionGrade)) {
            return positionGrade.getPositionGradeId();
        } else {
            return "-1";
        }

    }

    /**
     * M1查询职务级别测试列表[4920]
     * gen by moon at 10/16/2022, 11:07:11 AM
     */
    @Trace(operationName = "M1查询职务级别测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PositionGrade> queryPositionGradeTestDataList(PositionGrade positionGrade) {

        try {
            QueryWrapper<PositionGrade> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(positionGrade);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PositionGrade.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PositionGrade.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PositionGrade.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PositionGrade>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删职务级别测试[4922]
     * gen by moon at 10/16/2022, 11:07:11 AM
     */
    @Trace(operationName = "M1批量删职务级别测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeletePositionGradeTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
